#include "httpcurl.h"


#include <QCoreApplication>
#include <QString>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QString>
#include <QByteArray>
#include <QDebug>

HttpCurl::HttpCurl(QObject *parent) : QObject(parent)
{

}

QByteArray HttpCurl::post(QString url, QByteArray json)
{
    QNetworkRequest req(url);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    QNetworkAccessManager man;
    QNetworkReply *reply = man.post(req, json);

    while (!reply->isFinished())
    {
        qApp->processEvents();
    }

    QByteArray reponseBytes = reply->readAll();
    //qDebug() << reponseBytes << endl;
    return reponseBytes;
}

QByteArray HttpCurl::get(QString url)
{
    QNetworkRequest req(url);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    QNetworkAccessManager man;
    QNetworkReply *reply = man.get(req);

    while (!reply->isFinished())
    {
        qApp->processEvents();
    }

    QByteArray reponseBytes = reply->readAll();
    qDebug() << reponseBytes << endl;
    return reponseBytes;
}
